from bipy.scenes import Scene
from bipy.art import get as getart
from bipy.fonts import get as getfont
from psp2d import Controller, Image, Font
import math
class SlashScene(Scene):
    message = "Bipy FTW!"
    def get_signal_matrix(self):
        """
        A hard-coded map of signal-type to listening handle method
        """
        sm = super(SlashScene, self).get_signal_matrix()
        sm.update({
            'render' : self.render
            })
        return sm
        
    def enter(self):
        self.bgimg = getart('background.png')
        self.font = getfont('font.png')
        
        self.widths = []
        self.totalw = 0
        for c in self.message:
            self.widths.append(self.font.textWidth(c))
            self.totalw += self.font.textWidth(c)
            
        self.y = (272 - self.font.textHeight(self.message)) / 2
        self.x = (480 - self.totalw) / 2
        self.co = 240 / 128
        self.angle = 0.0
        
    def render(self):
        self.engine.buttons = Controller()
        x = self.x + ( self.engine.buttons.analogX * self.co )
        x = max(min(480, x), 0)
        self.engine.screen.clear()
        self.engine.screen.blit( self.bgimg )
        dx = 0
        for idx, c in enumerate( self.message ):
            self.engine.screen.draw_text(self.font, x + dx, self.y + 20 * math.sin( self.angle + 0.2 * idx), c)
            dx += self.widths[idx]
        self.angle += 0.2
            
        if self.engine.buttons.start:
            self.engine.pop_scene()

scene_class = SlashScene